package cn.rongdao.jrkabao.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v7.widget.AppCompatSeekBar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.SeekBar;

/**
 * Created by Wang on 17/9/21.
 */

public class CustomSeekBar extends AppCompatSeekBar {

   private String[] stringDisivionNum;//存储每个分段的中间值的数组
   private int disivionNumber ;      //总共需要分成的段;
   private int indexMinNumber;       //seekBar的起始数值;
   private int indexMaxNumber;      //seekBar的总数值;
   private float seekBarDisivionSize;//seekbar的每个分段所代表的数值多少;
   private int offset;      //每个分段所代表的数字大小;
   private int relaNumber;    //进度转化后的真实数值;
   private MeOnSeekBarChangeListener meOnSeekBarChangeListener;
   private boolean userAnima = true;  //是否开启动画;
   private int stripingSize;      //seekBar所能表示的数值,结束的数值减去开始的数值;
   private int currentIndex =-1;

    public CustomSeekBar(Context context) {
        super(context);
    }

    public CustomSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public int getOffset() {
        return offset;
    }

    public int getIndexMinNumber() {
        return indexMinNumber;
    }

    public void setMeOnSeekBarChangeListener(MeOnSeekBarChangeListener meOnSeekBarChangeListener) {
        this.meOnSeekBarChangeListener = meOnSeekBarChangeListener;
    }
/*
* startText 是seekBar开始的数值;
* endText   是seekBar所代表的总数值;
* deriverText 是seekBar的每个分段所表示的数值的大小;
* defaultText 是SeekBar所设置的默认进度数字(需转换);
* */
    public void  init(String startText, String endText, String deriverText, String defaultText){
        if(TextUtils.isEmpty(startText)|| TextUtils.isEmpty(endText)|| TextUtils.isEmpty(deriverText)){
            return;
        }
        int defaultNum = 0;
        try {
            indexMinNumber = Integer.parseInt(startText);
            indexMaxNumber = Integer.parseInt(endText);
            offset = Integer.parseInt(deriverText);
            if (offset == 0){
                return;
            }
            if(!TextUtils.isEmpty(defaultText)) {
                defaultNum = Integer.parseInt(defaultText);
            }
        }catch (NumberFormatException e){
            return;
        }
        stripingSize = indexMaxNumber - indexMinNumber;//进度所表示的数值范围
        disivionNumber =  stripingSize / offset;//分了几段
       // Log.e("WANG", "init: "+indexMaxNumber+"===="+indexMinNumber +"===="+offset+"====="+disivionNumber);
        this.setMax(stripingSize);
        seekBarDisivionSize = offset;
           stringDisivionNum = new String[disivionNumber];
           for (int i = 1; i <= stringDisivionNum.length; i++) {
               int rmb = indexMinNumber+i*offset;
               stringDisivionNum[i-1] = rmb+"";
              // Log.e("WANG", "分段区间: "+(i-1)+"   分段区间值="+rmb);
           }
           //这里处理有默认值的情况
           if(!TextUtils.isEmpty(defaultText)){
               //这个是算当前的值占几个进度值
               int currentProgress =(defaultNum -indexMinNumber);
               setAnimation(true,0,currentProgress);
               //this.setProgress(currentProgress);
           }
      this.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                //progress 是多少就是有多少金钱
               int devi =  progress /offset ;
              //  Log.e("WANG", "onProgressChanged: "+devi);
                int parseInt =indexMinNumber;
                    if (devi != currentIndex) {
                        currentIndex = devi;
                        if(devi >0) {
                            int index = devi-1;
                            if(index < stringDisivionNum.length) {
                                String centerNumber = stringDisivionNum[index];
                                parseInt = Integer.parseInt(centerNumber);
                                relaNumber = progress + indexMinNumber;
                            }
                        }
                        if (meOnSeekBarChangeListener != null) {
                                meOnSeekBarChangeListener.meOnProgressChanged(seekBar, progress, b, parseInt);
                        }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    public interface MeOnSeekBarChangeListener{
        void meOnProgressChanged(SeekBar seekBar, int progress, boolean b, int currentNumber);
    }
    private void setAnimation(boolean tage,float last, final float current) {
        if(tage) {
            ValueAnimator progressAnimator = ValueAnimator.ofFloat(last, current);
            progressAnimator.setDuration(500);
            progressAnimator.setInterpolator(new LinearInterpolator());
            progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float currentAngle = (float) animation.getAnimatedValue();
                    int rint = Math.round(currentAngle);
                    CustomSeekBar.this.setProgress(rint);
                }
            });
            progressAnimator.start();
        }
    }
}
